<?php
namespace app\m\controller;

use app\common\controller\FrontController;
use app\common\logic\SmsLogic;
use app\common\service\GoodsService;
use app\common\service\MemberService;
use think\captcha\Captcha;

class Index extends FrontController
{
    public function index()
    {
        return $this->fetch();
    }

    public function test()
    {
        return $this->fetch();
    }

    public function get_recommand(){
        $goodsService = new GoodsService();
        $jieguo = $goodsService->getRecommand();
        if($jieguo['err_code'] != 0){
            $this->error('商品获取失败');
        }
        else{
            $this->success('商品获取成功', '',$jieguo['data']);
        }

    }

    /**
     * 快速购买
     */
    public function quick(){
        // 读取 左侧 分类
        $goodsService = new GoodsService();
        $jieguo = $goodsService->getGoodsCategory(0);
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        $categoryList = $jieguo['data'];
        $this->assign('categoryList', $categoryList);
        $this->assign('categoryListJson', json_encode($categoryList,256));
        $memberService = new MemberService();
        $jieguo = $memberService->getMemberCartByMemberId($this->getMemberId());
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        $cartList = $jieguo['data'];
        $this->assign('cartList', $cartList);

        return $this->fetch();
    }

    public function quick_goods_detail(){
        $id = input('id');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $goodsService = new GoodsService();
        $jieguo = $goodsService->getGoods($id);
        if($jieguo['err_code'] != 0){
            $this->error('商品获取失败');
        }
        $goodsModel = $jieguo['data'];
//        dump($goodsModel);exit;

        // 请选择 规格的标题
        $spec_titles = '';
        if (!empty($goodsModel['has_option'])) {
            $tmp = [];
            foreach ($goodsModel['specs'] as $spec) {
                $tmp[] = $spec['title'];
            }
            $spec_titles = join('、', $tmp);
            unset($tmp);
        }
        $options = $goodsModel['options'];
        $fanhui = [];
        foreach ($options as $option) {
            $fanhui[] = [
                'id' => $option['id'],
                'title' => $option['title'],
                'thumb' => $option['thumb'],
                'price' => $option['market_price'],
                'stock' => $option['stock'],
                'specs' => '_' . $option['specs'] . '_',
            ];
        }
        $goodsModel['options'] = $fanhui;
        $goodsModel['spec_titles'] = $spec_titles;

        $this->success('成功','',$goodsModel);
    }
}
